package com.ocean.web.controller.system;

import cn.hutool.core.bean.BeanUtil;
import com.ocean.common.core.domain.entity.SysDept;
import com.ocean.common.utils.CreateWordUtils;
import com.ocean.common.utils.YamlUtil;
import com.ocean.system.service.ISysDeptService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ocean.common.core.controller.BaseController;
import com.ocean.common.core.domain.AjaxResult;
import com.ocean.common.core.domain.entity.SysUser;
import com.ocean.common.utils.StringUtils;
import com.ocean.framework.shiro.service.SysRegisterService;
import com.ocean.system.service.ISysConfigService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * 注册验证
 *
 * @author ocean
 */
@Controller
public class SysRegisterController extends BaseController
{
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ISysDeptService deptService;

    @GetMapping("/register")
    public String register()
    {
        return "register";
    }

    @PostMapping("/register")
    @ResponseBody
    public AjaxResult register(SysUser user, HttpServletRequest request)
    {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }
        String tempPath = this.getClass().getClassLoader().getResource( "templates").getPath();
//        String tempPath = request.getSession().getServletContext().getRealPath("").replace("\\","/")+"/ftl/";
        Map<String, Object> registerMap = BeanUtil.beanToMap(user);
        registerMap.put("contacts", user.getLxr());
        registerMap.put("idNumber", user.getSfzh());
        registerMap.put("telephone", user.getDh());
        registerMap.put("email", user.getYx());
        registerMap.put("depart", user.getSqdw());
        registerMap.put("unitProperty", user.getDwxz());
        registerMap.put("unitAddress", user.getTxdz());
        registerMap.put("cType", user.getZjlx());
        registerMap.put("cNumber", user.getZjhm());
        registerMap.put("postalCode", user.getYzbm());
        registerMap.put("downloadProducts", user.getXzcp());
        registerMap.put("productUse", user.getCpyt());
        String generateFilePath = (String) YamlUtil.loadYaml("sysConfig.yaml").get("ocean_register_doc_path");
        CreateWordUtils cwutils = new CreateWordUtils();
        String returnPath = cwutils.createWord(tempPath, generateFilePath, "OceanRegister", registerMap);
        returnPath = returnPath.substring(returnPath.lastIndexOf("/")+1);

/*
        user.setUserName(user.getLxr());user.setLoginName(user.getLxr());
        user.setPhonenumber(user.getDh());user.setEmail(user.getYx());
        //获取单位信息
        SysDept dept = new SysDept();dept.setDeptName(user.getSqdw());
        List<SysDept> deptList = deptService.selectDeptList(dept);
        if (CollectionUtils.isNotEmpty(deptList))
            user.setDeptId(deptList.get(0).getDeptId());
        user.setPassword("sys123@SYS");
        String msg = registerService.register(user);
*/
        return success(returnPath);
    }

    @GetMapping("/home/downOceanRegister")
    @ResponseBody
    public Object downOceanRegister(@RequestParam String fileName, HttpServletResponse response){
        CreateWordUtils cwUtils = new CreateWordUtils();
        String generateFilePath = (String) YamlUtil.loadYaml("sysConfig.yaml").get("ocean_register_doc_path");
        File file = new File(generateFilePath + fileName);
        cwUtils.exportFile(response,file);
        return null;
    }

    @PostMapping("/register1")
    @ResponseBody
    public AjaxResult ajaxRegister(SysUser user)
    {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }
        String msg = registerService.register(user);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }
}
